/**
 * 
 */
package cn.haona.game.simb.object.event;

import java.io.Serializable;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import cn.haona.db.RelevanceBean;
import cn.haona.game.simb.comm.util.Util;
import cn.haona.game.simb.object.Persistence;
import cn.haona.game.simb.object.PersistenceImpl;
import cn.haona.game.simb.object.bio.Fighter;
import cn.haona.game.simb.object.bio.Human;
import cn.haona.game.simb.object.bio.Npc;
import cn.haona.game.simb.object.bio.User;
import cn.haona.game.simb.object.bio.Fighter.FighterPO;
import cn.haona.game.simb.object.department.HAB;
import cn.haona.game.simb.object.god.MainManager;
import cn.haona.game.simb.object.team.FightTeamFighterOrg;

/**
 * @author Administrator
 * 
 */
public class FightEvent extends EventImpl {

	public static int FigherDistance = 100;
	//创建者
	private Long sponsor;

	// 团队领导者
	private Long leaderID;

	// 领导类型
	private int leaderType;
	
	//敌对团队的团员
	private List<Long> fighterMembers;

	// NPC队员
	private List<Long> listNpcID;

	// USER队员
	private List<Long> listUserID;

	/**
	 * 
	 */
	public FightEvent() {
		// TODO Auto-generated constructor stub
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see cn.haona.game.simb.object.Cell#initPO()
	 */
	public void initPO() {
		if (pO == null) {
			pO = new FightEventPO();
		}
	}
	
	@Override
	public boolean updateByPersis(long id) {
		boolean is=super.updateByPersis(id);
		
		FightEventPO fpo=getFightEventPO();
//		//设置创建者
//		setSponsor(fpo.getSponsor());
//		//敌方领导
//		setLeaderID(fpo.getLeaderID());
//		//敌方领导类型
//		setLeaderType(fpo.getLeaderType());
		//敌方团员
		setFighterMembers(getFighterMembersByID(fpo.getId(), fpo.getLeaderID()));
		
		return is;
	}

	/**
	 * 发生打斗事件
	 * 
	 * @param sponsor
	 *            发起者
	 * @param fightLeader
	 *            对方领导者
	 * @param leaderType
	 *            领导者类型
	 * @param listNpcID
	 *            对方队伍NPC们
	 * @param listUserID
	 *            对方队伍玩家们
	 * @return
	 */
	public static FightEvent occurrFightEvent(Long sponsor, Long fightLeader,
			int leaderType, List<Long> listNpcID, List<Long> listUserID) {
		FightEvent fightEvent = new FightEvent();

		// finghe

		return fightEvent;
	}

	/**
	 * 创建fight事件
	 * 
	 * @param sponsor
	 * @param ftto
	 * @return
	 */
	public static FightEvent createFightEvent(Long sponsor,
			FightTeamFighterOrg ftto) {
		HAB hab = MainManager.getInstance().getEntityManager().getHab();
		FightEvent fightEvent = new FightEvent();
		fightEvent.initPO();
		fightEvent.getFightEventPO().setFigherDistance(
				FightEvent.FigherDistance);
		fightEvent.getFightEventPO().setLeaderID(ftto.getLeaderID());
		fightEvent.getFightEventPO().setLeaderType(ftto.getLeaderType());
		fightEvent.getFightEventPO().setName(null);
		fightEvent.getFightEventPO().setResultDesc(null);
		fightEvent.getFightEventPO().setSponsor(sponsor);
		fightEvent.getFightEventPO().setStatus(
				FightEventPO.STATE_FIGHT_PREFIGHT);

		// 事件ID
		Long lFEID = fightEvent.getFightEventPO().persisMeFirstTime();
		// 创建领导
		// 领导类型
		String leaderClassName = null;
		if (ftto.getLeaderType() == Human.HUMAN_TYPE_NPC) {
			leaderClassName = Npc.class.getName();
		} else if (ftto.getLeaderType() == Human.HUMAN_TYPE_USER) {
			leaderClassName = User.class.getName();
		}
		// 领导
		Fighter fighterLeader = Fighter.createFighter(lFEID,
				ftto.getLeaderID(), ftto.getLeaderType(), leaderClassName);

		// 设置NPC
		createFightEvent_initNpcs(ftto, lFEID);
//		fightEvent.setListNpcID(createFightEvent_initNpcs(ftto, lFEID));
		// 设置USER们
		createFightEvent_initUsers(ftto, lFEID);
//		fightEvent.setListUserID(createFightEvent_initUsers(ftto, lFEID));

		// 反回来设置fightEvent的领导者
		fightEvent.getFightEventPO().setLeaderID(fighterLeader.getPO().getId());
		fightEvent.getFightEventPO().setLeaderType(
				fighterLeader.getFighterPO().getType());
//		fightEvent.setLeaderID(fighterLeader.getPO().getId());
//		fightEvent.setLeaderType(fighterLeader.getFighterPO().getType());
//		fightEvent.setSponsor(sponsor);
		
		//打斗事件为正在发生前
		fightEvent.getFightEventPO().setStatus(FightEventPO.STATE_FIGHT_PREFIGHT);
		fightEvent.getFightEventPO().persisMe();

		return fightEvent;

	}

	/**
	 * 初始化User们
	 * 
	 * @param ftto
	 * @param lFEID
	 * @return
	 */
	private static List<Long> createFightEvent_initUsers(
			FightTeamFighterOrg ftto, Long lFEID) {
		if (ftto.getListNpcID() != null) {
			HAB hab = MainManager.getInstance().getEntityManager().getHab();
			List<Long> listUser = new ArrayList<Long>();
			// NPC们
			if (ftto.getListNpcID() != null) {
				List<Long> listUserOrg = ftto.getListUserID();
				Iterator<Long> iteraNpc = listUserOrg.iterator();
				while (iteraNpc.hasNext()) {
					Long userID = iteraNpc.next();
					User user = hab.lookForUser(userID);
					// 创建打斗者
					Fighter fighter = Fighter.createFighter(lFEID, userID,
							Human.HUMAN_TYPE_USER, User.class.getName());
					listUser.add(fighter.getPO().getId());
				}
			}
			return listUser;
		}
		return null;
	}

	/**
	 * 初始化NPC们
	 * 
	 * @param ftto
	 * @param lFEID
	 * @return
	 */
	private static List<Long> createFightEvent_initNpcs(
			FightTeamFighterOrg ftto, Long lFEID) {
		if (ftto.getListNpcID() != null) {
			HAB hab = MainManager.getInstance().getEntityManager().getHab();
			List<Long> listNpc = new ArrayList<Long>();
			// NPC们
			if (ftto.getListNpcID() != null) {
				List<Long> listNpcOrg = ftto.getListNpcID();
				Iterator<Long> iteraNpc = listNpcOrg.iterator();
				while (iteraNpc.hasNext()) {
					Long npcID = iteraNpc.next();
					Npc npc = hab.lookForNpc(npcID);
					// 创建打斗者
					Fighter fighter = Fighter.createFighter(lFEID, npcID,
							Human.HUMAN_TYPE_NPC, Npc.class.getName());
					listNpc.add(fighter.getPO().getId());
				}
			}
			return listNpc;
		}
		return null;
	}
	
	
	/**
	 * 得到打斗事件----打斗前、正在打斗
	 * @param sponsor
	 * @return
	 */
	public static FightEvent getFigthEvent(Long sponsor) {

		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;

		try {
			String sql = "select " + FightEventPO.ID + " from "
					+ FightEventPO.TABLE_NAME + " where "
					+ FightEventPO.SPONSOR_ID + "=? and " + FightEventPO.STATUS
					+ "=" + FightEventPO.STATE_FIGHT_PREFIGHT+" or "+FightEventPO.STATUS
					+ "=" + FightEventPO.STATE_FIGHT_FIGHTING;
			conn = MainManager.getInstance().getPersistenceManager()
					.getDB_Connection();
			ps = conn.prepareStatement(sql);
			ps.setLong(1, sponsor);
			rs = ps.executeQuery();
			if (rs.next()) {
				Long iFEID = rs.getLong(1);
				FightEvent fe = (FightEvent) MainManager.getInstance()
						.getEntityManager().getCellInstance(
								FightEvent.class.getName(), iFEID);
				return fe;
			}
		} catch (Exception ex) {

		} finally {
			try {
				if (conn != null) {
					conn.close();
					conn = null;
				}
				if (ps != null) {
					ps.close();
					ps = null;
				}
				if (rs != null) {
					rs.close();
					rs = null;
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	public static FightEvent getFigthEvent(Long sponsor, Integer status) {

		return null;
	}

	public static List<FightEvent> getFigthEvent(Long sponsor, Integer status,
			int pageIndex, int pageSize) {
		return null;
	}
	
	/**
	 * 得到敌方战斗队员
	 * @param fightEventID
	 * @param leaderID
	 * @return
	 */
	public static List<Long> getFighterMembersByID(Long fightEventID,Long leaderID)
	{
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;

		List<Long> listFighter=new ArrayList<Long>();
		try
		{
			String sql="select "+FighterPO.ID+" from "+FighterPO.TABLE_NAME+
			" where "+FighterPO.EVENT_ID+"="+fightEventID+" and "+FighterPO.ID+"!="+leaderID;
			conn=MainManager.getInstance().getPersistenceManager().getDB_Connection();
			ps=conn.prepareStatement(sql);
			rs=ps.executeQuery();
			while(rs.next())
			{
				Long lID=rs.getLong(1);
				listFighter.add(lID);
			}
		}
		catch (Exception ex)
		{
			ex.printStackTrace();
		}
		finally
		{
			try
			{
				if (conn != null)
				{
					conn.close();
					conn = null;
				}
				if (ps != null)
				{
					ps.close();
					ps = null;
				}
				if (rs != null)
				{
					rs.close();
					rs = null;
				}
			}
			catch (Exception e)
			{
				e.printStackTrace();
			}
		}
		return listFighter;
	}
	
	/**
	 * 玩家动作
	 * @param id
	 * @return
	 */
	public Object userAct(int id)
	{
		return null;
	}

	/**
	 * 得到打斗事件持久化对象
	 * 
	 * @return
	 */
	public FightEventPO getFightEventPO() {
		return (FightEventPO) pO;
	}

	public Long getLeaderID() {
		return leaderID;
	}

	public void setLeaderID(Long leaderID) {
		this.leaderID = leaderID;
	}

	public int getLeaderType() {
		return leaderType;
	}

	public void setLeaderType(int leaderType) {
		this.leaderType = leaderType;
	}

	public List<Long> getListNpcID() {
		return listNpcID;
	}

	public void setListNpcID(List<Long> listNpcID) {
		this.listNpcID = listNpcID;
	}

	public List<Long> getListUserID() {
		return listUserID;
	}

	public void setListUserID(List<Long> listUserID) {
		this.listUserID = listUserID;
	}

	public Long getSponsor() {
		return sponsor;
	}

	public void setSponsor(Long sponsor) {
		this.sponsor = sponsor;
	}



	public List<Long> getFighterMembers() {
		return fighterMembers;
	}

	public void setFighterMembers(List<Long> fighterMembers) {
		this.fighterMembers = fighterMembers;
	}



	/**
	 * 打斗事件持久化对象
	 * 
	 * @author Administrator
	 * 
	 */
	public class FightEventPO extends PersistenceEvent implements Serializable{

		/**
		 * 
		 */
		private static final long serialVersionUID = 5413060739285948122L;

		final public static String TABLE_NAME = "OPT_FIGHT_EVENT";

		final public static String FIGHTER_LEADER_TYPE = "FIGHTER_LEADER_TYPE";
		final public static String FIGHTER_LEADER_ID = "FIGHTER_LEADER_ID";
		final public static String USER_IS_WIN = "USER_IS_WIN";
		final public static String FE_FIGHTER_DISTANCE = "FE_FIGHTER_DISTANCE";

		private Long leaderID;
		private Integer leaderType;
		private Integer userWin;
		private Integer figherDistance;

		//打斗发生前
		final public static int STATE_FIGHT_PREFIGHT = 2;
		//打斗正在发生
		final public static int STATE_FIGHT_FIGHTING = 1;

		/**
		 * 设置反射关联
		 * 
		 * @param reflectRelevance
		 */
		public void setReflectRelevance(
				HashMap<String, RelevanceBean> reflectRelevance) {
			super.setReflectRelevance(reflectRelevance);
			reflectRelevance.put(FIGHTER_LEADER_TYPE, new RelevanceBean(
					"leaderType", FIGHTER_LEADER_TYPE));
			reflectRelevance.put(FIGHTER_LEADER_ID, new RelevanceBean(
					"leaderID", FIGHTER_LEADER_ID));
			reflectRelevance.put(USER_IS_WIN, new RelevanceBean("leaderType",
					USER_IS_WIN));
			reflectRelevance.put(FE_FIGHTER_DISTANCE, new RelevanceBean(
					"figherDistance", FE_FIGHTER_DISTANCE));
		}

		public Long getLeaderID() {
			return leaderID;
		}

		public void setLeaderID(Long leaderID) {
			this.leaderID = leaderID;
		}

		public Integer getLeaderType() {
			return leaderType;
		}

		public void setLeaderType(Integer leaderType) {
			this.leaderType = leaderType;
		}

		public Integer getUserWin() {
			return userWin;
		}

		public void setUserWin(Integer userWin) {
			this.userWin = userWin;
		}

		public Integer getFigherDistance() {
			return figherDistance;
		}

		public void setFigherDistance(Integer figherDistance) {
			this.figherDistance = figherDistance;
		}
	}

}
